/*
 * Famon (http://code.google.com/p/famon/)
 * Copyright (c) 2010 Carl Crous.
 *
 * This file is part of Famon.
 *
 * Famon is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Famon is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Famon.  If not, see <http://www.gnu.org/licenses/>.
 */

package za.org.foobar.famon.frontend.client.util;

import za.org.foobar.famon.frontend.client.framework.FastGrid;
import za.org.foobar.famon.frontend.client.framework.SortableFastGrid;

/**
 * Helper base class to make it easier to use FastGrid's with EMS. It provides
 * helper methods to generate the HTML for a row.
 */
public abstract class FamonRenderer
        implements FastGrid.Renderer, SortableFastGrid.Sorter {

    protected void tr(int row, StringBuffer html) {
        tr(row, null, html);
    }

    protected void tr(int row, String cls, StringBuffer html) {
        html.append("<tr class=\"");
        if (cls != null) {
            html.append(cls);
            html.append(' ');
        }
        html.append((row & 1) == 0 ? "even" : "odd");
        html.append("\">");
    }

    protected void trEnd(StringBuffer html) {
        html.append("</tr>");
    }

    protected void td(StringBuffer html) {
        html.append("<td>&nbsp;</td>");
    }

    protected void td(String text, StringBuffer html) {
        td(text, html, false);
    }

    protected void td(String text, StringBuffer html, boolean noBreak) {
        if (noBreak) {
            html.append("<td NOWRAP>");
        } else {
            html.append("<td>");
        }
        html.append(text);
        html.append("</td>");
    }

    protected void td(String text, int colspan, StringBuffer html) {
        html.append("<td colspan=\"");
        html.append(colspan);
        html.append("\">");
        html.append(text);
        html.append("</td>");
    }

    protected void td(String text, String style, StringBuffer html) {
        if (style == null) {
            td(text, html);
        } else {
            html.append("<td class=\"");
            html.append(style);
            html.append("\">");
            html.append(text);
            html.append("</td>");
        }
    }

    protected void tdLink(String href, String text, StringBuffer html) {
        if (href == null) {
            td(ClientUtil.NONE, html);
        } else {
            html.append("<td><a class=\"codeLink\" href=\"");
            html.append(href);
            html.append("\">");
            html.append(text);
            html.append("</a></td>");
        }
    }

    protected void tdCodeName(String code, String name, StringBuffer html) {
        if (code == null) {
            td(ClientUtil.NONE, html);
        } else {
            html.append("<td><a class=\"codeLink\" href=\"#");
            html.append(code);
            html.append("\">");
            html.append(name);
            html.append("</a></td>");
        }
    }

    protected void tdLeave(String username, String name, StringBuffer html) {
        if (username == null) {
            td(ClientUtil.NONE, html);
        } else {
            tdCodeName("leave?username=" + username, name, html);
        }
    }

    protected void tdCodeName(String code, String name, String style, StringBuffer html) {
         if (code == null) {
            td(ClientUtil.NONE, html);
        } else {
            html.append("<td class=\"" + style + "\"><a class=\"codeLink\" href=\"#");
            html.append(code);
            html.append("\">");
            html.append(name);
            html.append("</a></td>");
        }
    }

    protected void tdCodeNameEmail(String code, String name, StringBuffer html) {
        if (code == null || code.length() == 0) {
            td(ClientUtil.NONE, html);
        } else {
            html.append("<td><a href=\"mailto:");
            html.append(code);
            html.append("\">");
            html.append(name);
            html.append("</a></td>");
        }
    }

    protected void linkCodeName(String code, String name, StringBuffer html) {
        if (code == null) {
            html.append(ClientUtil.NONE);
        } else {
            html.append("<a class=\"codeLink\" href=\"#");
            html.append(code);
            html.append("\">");
            html.append(name);
            html.append("</a>");
        }
    }

    protected void td(boolean on, StringBuffer html) {
        html.append("<td>");
        html.append(on ? ClientUtil.YES : ClientUtil.NO);
        html.append("</td>");
    }

    protected int comp(Comparable a, Comparable b) {
        if (a == null) {
            return b == null ? 0 : -1;
        }
        if (b == null) {
            return +1;
        }
        return a.compareTo(b);
    }

    protected int comp(boolean a, boolean b) {
        if (a && !b) {
            return -1;
        }
        if (!a && b) {
            return +1;
        }
        return 0;
    }

    /** Compare strings that represent doubles. */
    protected int compNum(String a, String b) {
        if (a == null) {
            return b == null ? 0 : -1;
        }
        if (b == null) {
            return +1;
        }
        double da = Double.parseDouble(a);
        double db = Double.parseDouble(b);
        return da < db ? -1 : da > db ? +1 : 0;
    }

    public boolean isSortable(int col) {
        return true;
    }

    /**
     * Get the title used when the data on the grid is downloaded or null
     * for default.
     */
    public String getTitle() {
        return null;
    }

}
